package com.mundus.engine;

import java.io.File;
import org.ini4j.Ini;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import com.mundus.engine.assets.Log;

public class Settings {
	public enum Resolution{
		Res_1920x1080(1920,1080),
		Res_1600x900(1600,900),
		Res_1366x768(1366,768),
		Res_1280x720(1280,720),
		Res_1152x864(1152,864),
		Res_1024x768(1024,768),
		Res_800x600(800,600);
		
		private int _width = 800;
		private int _height = 600;
		
		public int getWidth(){ return _width;}
		public int getHeight(){ return _height;}
		public String getStringLiteral(){ return _width + "x" + _height;}
		
		Resolution(int width, int height){
			_width = width;
			_height = height;
		}
		
		public static Resolution parseResolution(String resolution){
			for(Resolution res : Resolution.values())
				if (resolution.equalsIgnoreCase(res.getStringLiteral()))
					return res;
			
			return Resolution.Res_800x600;
		}
		
		public static String[] getResolutionValues(){
			String[] resValues = new String[Resolution.values().length];
			for(int i = 0; i < resValues.length; i ++)
				resValues[i] = Resolution.values()[i].getStringLiteral();
			
			return resValues;
		}
		
	}
	
	public static boolean DISPLAYLAUNCHER = true;
	
	private static boolean _fullscreen = false;
	private static boolean _vsync = false;
	private static int _fpsLimit = 60;
	private static String _title;
	private static Resolution _resolution;
	
	public static boolean getFullscreen() { return _fullscreen;}
	public static boolean getVsync(){ return _vsync;}
	public static float getAspectRatio(){ return (float)_resolution.getWidth()/(float)_resolution.getHeight(); }
	public static int getFPSLimit(){ return _fpsLimit != 0 ? _fpsLimit : 3000; }
	public static String getTitle(){ return _title;}
	public static Resolution getResolution(){ return _resolution;}	
	
	public static void initSettings(){
		try{			
			 String path = new File(".").getCanonicalPath() + "\\assets\\settings.ini";
			 Ini settingsIni = new Ini(new File(path));
			 _resolution = Resolution.parseResolution(settingsIni.get("DISPLAY","resolution"));
			 _fullscreen = Boolean.parseBoolean(settingsIni.get("DISPLAY", "fullscreen"));
			 _vsync = Boolean.parseBoolean(settingsIni.get("DISPLAY", "vsync"));
			 _fpsLimit = Integer.parseInt(settingsIni.get("DISPLAY", "fpsLimit"));
			 
			 DISPLAYLAUNCHER = Boolean.parseBoolean(settingsIni.get("LAUNCHER", "showOnStart"));
			 _title = settingsIni.get("LAUNCHER", "title");
			 
		}
		catch(Exception e){
			Log.error(e);
		}
	}
	
	public static DisplayMode getMatchingDisplayMode(){
		try{
			DisplayMode targetDisplayMode = null;
			DisplayMode[] availableModes = Display.getAvailableDisplayModes();
			int freq = 0;
			
			if(_fullscreen){
				for(DisplayMode mode : availableModes){
					DisplayMode current = mode;
					
					if ((current.getWidth() == _resolution.getWidth()) && (current.getHeight() == _resolution.getHeight())) {
					    if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
					        if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
					        	targetDisplayMode = current;
					        	freq = targetDisplayMode.getFrequency();
	                        }
	                    }
	
					    if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) &&
			                        (current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
			                            targetDisplayMode = current;
			                            break;
	                    }
					}
				}	
	        } else {
	            targetDisplayMode = new DisplayMode(_resolution.getWidth(),_resolution.getHeight());
	        }

	        if (targetDisplayMode == null) {
		            Log.error("Failed to find value mode: "+_resolution.getWidth()+"x"+_resolution.getHeight()+" fs="+_fullscreen);
		            return null;
	        }
	        else
	        	return targetDisplayMode;
	        
		}
		catch(LWJGLException e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static void updateSettings(String resolution, boolean vsync, boolean fullscreen, int framelimit, boolean showOnStart){
		try{			
			 String path = new File(".").getCanonicalPath() + "\\assets\\settings.ini";
			 Ini settingsIni = new Ini(new File(path));
			 settingsIni.clear();
			 settingsIni.add("DISPLAY");
			 settingsIni.put("DISPLAY", "resolution", resolution);
			 settingsIni.put("DISPLAY", "fullscreen", fullscreen);
			 settingsIni.put("DISPLAY", "vsync", vsync);
			 settingsIni.put("DISPLAY", "fpsLimit", framelimit);
			 
			 settingsIni.put("LAUNCHER","showOnStart", showOnStart);
			 settingsIni.put("LAUNCHER","title",_title);
			 settingsIni.store(new File(path));			 
		}
		catch(Exception e){
			Log.error(e);
		}
	}
}
